home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_085 / csh / sort.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  1KB  |  72 lines

  1.  
  2. /*
  3.  * sort.c
  4.  *
  5.  *
  6.  * version 2.06M (Manx Version and Additions) by Steve Drew 28-May-87
  7.  *
  8.  * sort has been modified via addition of slashcmp which provides correct
  9.  * sorting of file names in tree/alphabetical order. This was needed
  10.  * due to the recursive wild carding support I added in expand(). /Steve
  11.  */
  12.  
  13. QuickSort(av, n)
  14. char *av[];
  15. int n;
  16. {
  17.    int b;
  18.  
  19.    if (n > 0) {
  20.       b = QSplit(av, n);
  21.       QuickSort(av, b);
  22.       QuickSort(av+b+1, n - b - 1);
  23.    }
  24. }
  25.  
  26.  
  27. /*
  28.  * QSplit called as a second routine so I don't waste stack on QuickSort's
  29.  * recursivness.
  30.  */
  31.  
  32. QSplit(av, n)
  33. char *av[];
  34. int n;
  35. {
  36.    int i, b;
  37.    char *element, *scr;
  38.  
  39.    element = av[0];
  40.    for (b = 0, i = 1; i < n; ++i) {
  41.       if (slashcmp(av[i],element) < 0) {
  42.      ++b;
  43.      scr = av[i]; av[i] = av[b]; av[b] = scr;
  44.       }
  45.    }
  46.    scr = av[0]; av[0] = av[b]; av[b] = scr;
  47.    return (b);
  48. }
  49.  
  50. slashcmp(a,b)
  51. char *a,*b;
  52. {
  53.    char *ap,*bp;
  54.    int b_slash=0,a_slash=0,c;
  55.    char *index();
  56.  
  57.    if (ap = index(a,'/')) ++a_slash;
  58.    if (bp = index(b,'/')) ++b_slash;
  59.  
  60.    if (a_slash && b_slash) { /* both contain slashes */
  61.        *bp = *ap = '\0';
  62.        c = strcmp(a,b); /* just compare before slash */
  63.        *bp = *ap = '/';
  64.        if (c == 0) c = slashcmp(ap+1,bp+1); /* recursive */
  65.        return(c);
  66.    }
  67.    if (!a_slash && !b_slash)  /* neither have slashes so just cmp */
  68.        return(strcmp(a,b));
  69.    return(a_slash - b_slash);
  70. }
  71.  
  72.